// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include <gtest/gtest.h>

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Result/IResultDomainsService.h"
#include "DBManager/IService/Result/IntegStress/ICTRIA3IntegStressService.h"
#include "DBManager/IService/Result/IntegStress/IQUAD4IntegStressService.h"
#include "DBManager/UnitTest/HDF5/TestDB.h"

using namespace SG::DataStructure::FEM;
using namespace SG::DataStructure::Post;
using namespace SG::DBManager;

/*
结果仓库的单元测试测试4个接口：
    1）添加数据；
    2）通过DomainId获取整个实体（节点或单元）上的结果数据；
    3）通过DomainId与实体ID获取单个实体上的结果数据；
    4）获取某个domainId内的结果数量

*/
TEST (O_ResultTest, ResultDomainsService)
{
    DB::ServiceFactory->ClearDatabase ();
    auto service = DB::ServiceFactory->get<IResultDomainsService> ();
    // // 保存矩阵
    SG::DataStructure::Post::ResultDomains saveData;
    saveData.m_id       = 1;
    saveData.m_Analysis = SG::DataStructure::Post::AnalysisType::ANALYSISTYPE_Static;
    saveData.m_Subcase  = 1;
    service->Add (saveData);

    saveData.m_id       = 3;
    saveData.m_Analysis = SG::DataStructure::Post::AnalysisType::ANALYSISTYPE_Static;
    saveData.m_Subcase  = 5;
    ASSERT_EQ (service->Add (saveData), SG::DataStructure::Common::Status::STATUS_Success);

    saveData.m_id       = 2;
    saveData.m_Analysis = SG::DataStructure::Post::AnalysisType::ANALYSISTYPE_Static;
    saveData.m_Subcase  = 7;
    // 是否获取成功
    // ASSERT_TRUE
    ASSERT_EQ (service->Add (saveData), SG::DataStructure::Common::Status::STATUS_Failure);

    SG::DataStructure::Post::ResultDomains domain;
    ASSERT_EQ (service->Find ({ SG::DataStructure::FEM::SOLType::SOL_SESTATIC, 5 }, domain),
               SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (domain.m_id, 3);

    ASSERT_EQ (service->Find ({ SG::DataStructure::FEM::SOLType::SOL_SESTATIC, 6 }, domain),
               SG::DataStructure::Common::Status::STATUS_Failure);

    // 测试找不到时新建
    ASSERT_EQ (service->GetDomainId ({ SG::DataStructure::FEM::SOLType::SOL_SESTATIC, 6 }, saveData.m_id),
               SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (saveData.m_id, 4);

    ASSERT_EQ (service->GetAllData ().size (), 3);
}

TEST (O_ResultTest, CTRIA3IntegStressService)
{
    DB::ServiceFactory->ClearDatabase ();

    // 保存矩阵
    std::vector<SG::Algebra::Matrixd> saveData;
    for (size_t i = 0; i < 10; i++)
    {
        saveData.emplace_back (SG::Algebra::Matrixd (6, 3, i));
    }

    auto service = DB::ServiceFactory->get<ICTRIA3IntegStressService> ();
    service->AddBySubcaseId (1, saveData);

    // 查询矩阵
    std::vector<SG::Algebra::Matrixd> result;

    // 是否获取成功
    ASSERT_TRUE (service->FindBySubcaseId (1, result));
    ASSERT_EQ (result.size (), 10);
    ASSERT_EQ (saveData, result);
}

TEST (O_ResultTest, QUAD4IntegStressService)
{
    DB::ServiceFactory->ClearDatabase ();

    // 保存矩阵
    std::vector<SG::Algebra::Matrixd> saveData;
    for (size_t i = 0; i < 10; i++)
    {
        saveData.emplace_back (SG::Algebra::Matrixd (6, 4, i));
    }

    auto service = DB::ServiceFactory->get<IQUAD4IntegStressService> ();
    service->AddBySubcaseId (1, saveData);

    // 查询矩阵
    std::vector<SG::Algebra::Matrixd> result;

    // 是否获取成功
    ASSERT_TRUE (service->FindBySubcaseId (1, result));
    ASSERT_EQ (result.size (), 10);
    ASSERT_EQ (saveData, result);
}